<?php
/**
 * 用户收藏服务类
 *
 */
class CollectService extends Service {
	
	/**
	 * 查询当前登陆用户的收藏分页数据。
	 * @param $stype		查看的收藏类型	'tobesend'--将要送出的  'desirous'--希望得到的
	 */
	public function search4User($stype=Dict::TOBESEND,$uid=null,$limit=15){
		if(empty($uid))$uid=$_SESSION['mid'];
		$map['user_id']=$uid;
		$map['collect_type']=$stype;
		$map['is_delete']='N';
		$collectpage = M('ShelfCollect')->kPageQuery($map,' ctime desc ',$limit);
		if(!empty($collectpage)&&!empty($collectpage['data'])){
			foreach ($collectpage['data'] as $vo){
				$ary_sid[] = $vo['shelf_id'];
			}
		}
		$where['shelf_id'] = array('in',$ary_sid);
		//$where['partner_enable'] 	= 'Y';$where['goods_enable'] 		= 'Y';$where['shelf_enable'] 		= 'Y';
		//$where['partner_is_delete'] = 'N';$where['goods_is_delete'] 	= 'N';$where['shelf_is_delete'] 	= 'N';
		$plist = M('ViewShelf')->where($where)->findAll();//->kPageQuery($where,'',$limit);
		foreach ($plist as $pvo){
			$extData[$pvo['shelf_id']] = $pvo ;
		}
		$collectpage['extData']	=	$extData;
		return $collectpage;
	}
	
	
	/**
	 * 查询当前登陆用户的是否已收藏该书局。
	 * @param $stype		查看的收藏类型	'tobesend'--将要送出的  'desirous'--希望得到的
	 */
	public function select4UserIfCollect($ary_shelfids,$stype=Dict::TOBESEND){
		$map['user_id']=$_SESSION['mid'];
		$map['collect_type']=$stype;
		$map['shelf_id']=array('in',$ary_shelfids);
		$map['is_delete']='N';
		$collects = M('ShelfCollect')->where($map)->findAll();

		foreach ($collects as $pvo){
			$extData[$pvo['shelf_id']] = $pvo;
		}
		//$collectpage['extData']	=	$extData;
		return $extData;
	}
	
	/**
	 * 计算总数
	 * @param unknown_type $stype
	 */
	public function count4User($stype,$uid){
		if(empty($uid))$uid=$_SESSION['mid'];
		$map['user_id']=$uid;
		$map['collect_type']=$stype;
		$map['is_delete']='N';
		$num = M('ShelfCollect')->where($map)->field(' count(*) as totalno')->find();
		return $num['totalno'];
	}
	
	public function add4User($stype,$shelf_id){
		$result['status'] 	= -1;
		$result['msg'] 		= '数据有误！';
		if(empty($stype)||empty($shelf_id))return $result;
		if($stype!=Dict::TOBESEND&&$stype!=Dict::DESIROUS)return $result;
		$map['user_id']=$_SESSION['mid'];
		$map['collect_type']=$stype;
		$map['shelf_id']=$shelf_id;
		//dump($map);
		$collect = M('ShelfCollect')->where($map)->find();
		if(empty($collect)){
			$map['ctime']=time();
			$id = M('ShelfCollect')->add($map);
			if($id){
				$result['status'] 	= 1;	$result['msg'] 		= $stype==Dict::DESIROUS?'想要的礼物添加成功！':'收藏成功！';
				return $result;
			}else{
				$result['status'] 	= 0;	$result['msg'] 		= $stype==Dict::DESIROUS?'想要的礼物添加失败！':'收藏失败！';
				return $result;
			}
		}else{
			if($collect['is_delete']=='Y'){
				$collect['is_delete']='N';
				M('ShelfCollect')->save($collect);
				$result['status'] 	= 1;	$result['msg'] 		= $stype==Dict::DESIROUS?'想要的礼物添加成功！':'收藏成功！';
				return $result;
			}else{
				$result['status'] 	= -1;	$result['msg'] 		= $stype==Dict::DESIROUS?'想要的礼物已添加！':'已收藏！';
				return $result;
			}
		}
	}
	
	
	public function delete4User($stype,$shelf_id){ 
		$result['status'] 	= -1;	$result['msg'] 		= '数据有误！';
		if(empty($shelf_id))return $result;
		$map['collect_type']=$stype;
		$map['shelf_id']=$shelf_id;
		$map['user_id']=$_SESSION['mid'];
		$map['is_delete']='N';
		$data['is_delete']='Y';
		$no = M('ShelfCollect')->where($map)->save($data);
		
		if($no){
			$result['status'] 	= 1;	$result['msg'] 		= $stype==Dict::DESIROUS?'想要的礼物删除成功！':'删除收藏成功！';
			return $result;
		}else{
			$result['status'] 	= 0;	$result['msg'] 		= $stype==Dict::DESIROUS?'想要的礼物删除失败！':'删除收藏失败！';
			return $result;
		}
	}
	
	//服务初始化
	public function init() {
	}

	//运行服务，系统服务自动运行
	public function run() {
	}
	
	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}
	
	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>